package com.sjgtw.cloud.system.service.impl;

import com.sjgtw.cloud.system.domain.orm.UserEntity;
import com.sjgtw.cloud.system.mapper.SysUserPostMapper;
import com.sjgtw.cloud.system.mapper.SysUserProvisioningMapper;
import com.sjgtw.cloud.system.mapper.SysUserRoleMapper;
import com.sjgtw.cloud.system.service.ISysUserProvisioningService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>Title: 用户信息管理--服务层--接口实现</p>
 * <p>Description: rsf数据同步</p>
 * @author Admin
 * @date 2020-7-7
 */
@Service
public class SysUserProvisioningServiceImpl implements ISysUserProvisioningService
{
	private static final Logger log = LoggerFactory.getLogger(SysUserProvisioningServiceImpl.class);

	@Autowired
	private SysUserProvisioningMapper userMapper;
	
	@Autowired
	private SysUserRoleMapper userRoleMapper;

	@Autowired
	private SysUserPostMapper userPostMapper;

	/**
	 * 根据条件分页查询用户列表
	 * 
	 * @param user 用户信息
	 * @return 用户信息集合信息
	 */
	@Override
	public List<UserEntity> selectUserList(UserEntity user) {
		return userMapper.selectUserList(user);
	}

	/**
	 * 通过用户名查询用户
	 * 
	 * @param userName 用户名
	 * @return 用户对象信息
	 */
	@Override
	public UserEntity selectUserByUserName(String userName) {
		return userMapper.selectUserByUserName(userName);
	}

	/**
	 * 新增保存用户信息
	 * 
	 * @param user 用户信息
	 * @return 结果
	 */
	@Override
	public int insertUser(UserEntity user) {
		// 新增用户信息
		int rows = userMapper.insertUser(user);
		return rows;
	}

	/**
	 * 修改保存用户信息
	 * 
	 * @param user 用户信息
	 * @return 结果
	 */
	@Override
	public int updateUser(UserEntity user) {
		return userMapper.updateUser(user);
	}

	/**
	 * 修改用户状态
	 * 
	 * @param user 用户信息
	 * @return 结果
	 */
	@Override
	public int updateUserStatus(UserEntity user) {
		return userMapper.updateUserStatus(user);
	}
	
	/**
	 * 逻辑删除用户状态
	 * 
	 * @param user 用户信息
	 * @return 结果
	 */
	@Override
	@Transactional
	public int updateUserDelFlag(UserEntity user) {
		Long userId = user.getUserId();
		// 删除用户与角色关联
		userRoleMapper.deleteUserRoleByUserId(userId);
		// 删除用户与岗位表
		userPostMapper.deleteUserPostByUserId(userId);
		return userMapper.updateUserDelFlag(user);
	}
}
